package top.dancun.easbaseinfo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import top.dancun.common.utils.PageUtils;
import top.dancun.common.utils.Query;
import top.dancun.easbaseinfo.dao.ClassDao;
import top.dancun.easbaseinfo.entity.ClassEntity;
import top.dancun.easbaseinfo.service.ClassService;

import java.util.List;
import java.util.Map;
import java.util.Objects;


@Service("classService")
public class ClassServiceImpl extends ServiceImpl<ClassDao, ClassEntity> implements ClassService {

    @Override
    public PageUtils queryPage(Map<String, Object> params, Map<String,String> dataMap) {
        IPage<ClassEntity> page = new Query<ClassEntity>().getPage(params);
        LambdaQueryWrapper<ClassEntity> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.like(Objects.nonNull(dataMap.get("name")),
                ClassEntity::getClassCode,
                params.get("name"))
                .eq(Objects.nonNull(dataMap.get("startYear")),
                        ClassEntity::getStartYear,
                        dataMap.get("startYear"))
                .like(Objects.nonNull(dataMap.get("classLeader")),
                        ClassEntity::getClassLeader,
                        dataMap.get("classLeader"))
                .eq(Objects.nonNull(dataMap.get("studentLength")),
                        ClassEntity::getStudentLength,
                        dataMap.get("studentLength"))
                .eq(Objects.nonNull(dataMap.get("majorId")),
                        ClassEntity::getMajorId,
                        dataMap.get("majorId"));
         page = this.baseMapper.list(page,lambdaQueryWrapper);

        return new PageUtils(page);
    }

    @Override
    public List<ClassEntity> query(Map<String, String> dataMap) {
        LambdaQueryWrapper<ClassEntity> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.like(Objects.nonNull(dataMap.get("name")),
                        ClassEntity::getClassCode,
                        dataMap.get("name"))
                .eq(Objects.nonNull(dataMap.get("startYear")),
                        ClassEntity::getStartYear,
                        dataMap.get("startYear"))
                .like(Objects.nonNull(dataMap.get("classLeader")),
                        ClassEntity::getClassLeader,
                        dataMap.get("classLeader"))
                .eq(Objects.nonNull(dataMap.get("studentLength")),
                        ClassEntity::getStudentLength,
                        dataMap.get("studentLength"))
                .eq(Objects.nonNull(dataMap.get("majorId")),
                        ClassEntity::getMajorId,
                        dataMap.get("majorId"));
        return this.baseMapper.listNoPage(lambdaQueryWrapper,dataMap.get("majorId"));
    }

    @Override
    public List<ClassEntity> queryByIds(List<Integer> ids) {
        LambdaQueryWrapper<ClassEntity> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.in(ClassEntity::getClassId,ids);
        return this.baseMapper.listNoPage(lambdaQueryWrapper,null);
    }

    @Override
    public List<ClassEntity> list(Map<String,Object> params) {

        LambdaQueryWrapper<ClassEntity> wrapper = Wrappers.lambdaQuery();
        if (Objects.nonNull(params.get("majorId"))){
            Integer id = Integer.parseInt(params.get("majorId").toString());
            wrapper.eq(ClassEntity::getMajorId , id);
        }
        return this.baseMapper.selectList(wrapper);
    }

}